﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SilverPOP.Library.BusinessEntities;
using SilverPOP.Library.BusinessLogicLayer;
using Utilities;
using SilverPOP.Data.Process.BLL;
using SilverPOP.Library.DataAccessLayer;
using SilverPOP.Data.Process.Entity;

namespace SilverPOP.Data.Process.OperationUnit
{
    public abstract class DataAndSchemaProcess
    {
        #region Fields
        private static SilverPopAccount _accountInstance=null;

        protected String folderName = String.Empty;
        protected String directoryName = String.Empty;
        protected String successFileNameWithPath = String.Empty;
        protected String errorFileNameWithPath = String.Empty;
        protected TimeSpan processStartingTime;
        protected long campaignId = 0;
        #endregion


        #region Singleton Properties
        public  SilverPopAccount InstanceSilverPOPAccount
        {
            get
            {
                if (_accountInstance == null)
                {

                    var db = new DatabaseContext(SilverPopConnectionInfo.SqlConnectionString);
                  //  _accountInstance = new SilverPopAccountBLL().GetSilverPopDefaultAccount();
                    _accountInstance = new SilverPopAccountBLL(db).GetSilverPopDefaultAccount();

                    if(_accountInstance == null)
                        throw new Exception("SilverPOP account not found.");
                    else
                      return _accountInstance;
                }
                else
                {
                    return _accountInstance;
                }
            }
        }
        #endregion

        #region Constructor
        public DataAndSchemaProcess()
        {

        }
        #endregion

        #region Abstract Methods
        public abstract  void ExecuteOperation();
        #endregion

        #region Common Methods Used By All Child Class

        #region Create Folder
        protected void CreateFolder(String directoryNameParam)
        {
            this.folderName = DateTime.Now.ToShortDateString().Replace("/", "-") + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second;
            this.directoryName = directoryNameParam + @"\" + this.folderName;
            this.successFileNameWithPath = this.directoryName + @"\" + "success.txt";
            this.errorFileNameWithPath = this.directoryName + @"\" + "error.txt";

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
        }

        protected String CreateSQLReferenceFolder(String directoryNameParam)
        {
            if (String.IsNullOrEmpty(this.folderName))
            {
                this.folderName = DateTime.Now.ToShortDateString().Replace("/", "-") + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second;
            }
            
            String directoryName = directoryNameParam + @"\" + this.folderName;

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            return directoryName;
        }

        #endregion

        #region Calculate Total Processing Time
        protected  void CalculateTotalProcessingTime()
        {
            int totalProcessingHours = DateTime.Now.TimeOfDay.Subtract(this.processStartingTime).Hours;
            int totalProcessingTime = DateTime.Now.TimeOfDay.Subtract(this.processStartingTime).Minutes;
            int totalProcessingSecond = DateTime.Now.TimeOfDay.Subtract(this.processStartingTime).Seconds;


            Console.WriteLine("Total Processing Time Taken : " + totalProcessingHours + " hour(s) " + totalProcessingTime + " minute(s) " + totalProcessingSecond + " Second(s) ");

            FileWriter.WriteToFile(this.successFileNameWithPath, "Total Processing Time Taken : " + totalProcessingHours + " hour(s) " + totalProcessingTime + " minute(s) " + totalProcessingSecond + " Second(s) ");

        }
        #endregion

        #region Log Error
        protected void LogError(Exception ex)
        {
            if (ex.InnerException != null)
            {
                Console.WriteLine(ex.InnerException.Message);
                FileWriter.WriteToFile(this.errorFileNameWithPath, ex.InnerException.Message);
            }
            else
            {
                Console.WriteLine(ex.Message);
                FileWriter.WriteToFile(this.errorFileNameWithPath, ex.Message);
            }
        }

        protected void LogError(Exception ex,String rowNumber)
        {
            if (ex.InnerException != null)
            {
                Console.WriteLine(ex.InnerException.Message+" "+rowNumber);
                FileWriter.WriteToFile(this.errorFileNameWithPath, ex.InnerException.Message + " " + rowNumber);
            }
            else
            {
                Console.WriteLine(ex.Message + " " + rowNumber);
                FileWriter.WriteToFile(this.errorFileNameWithPath, ex.Message + " " + rowNumber);
            }
        }

        #endregion

        #endregion


    }
}
